package com.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.common.Result;
import com.car.entity.Customer;
import com.car.service.CustomerService;
import com.car.vo.CustomerDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户控制器
 *
 * @author Car System
 */
@Tag(name = "客户管理")
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerService customerService;

    @Operation(summary = "分页查询客户")
    @GetMapping("/page")
    public Result<IPage<Customer>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        IPage<Customer> page = customerService.page(current, size, keyword);
        return Result.success(page);
    }

    @Operation(summary = "查询所有客户")
    @GetMapping("/list")
    public Result<List<Customer>> list() {
        List<Customer> list = customerService.list();
        return Result.success(list);
    }

    @Operation(summary = "根据ID查询客户")
    @GetMapping("/{id}")
    public Result<Customer> getById(@PathVariable Long id) {
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    @Operation(summary = "获取客户详情（含车辆、保养、维修记录）")
    @GetMapping("/{id}/detail")
    public Result<CustomerDetailVO> getDetail(@PathVariable Long id) {
        CustomerDetailVO detail = customerService.getDetail(id);
        return Result.success(detail);
    }

    @Operation(summary = "新增客户")
    @PostMapping
    public Result<Void> save(@RequestBody Customer customer) {
        customerService.save(customer);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新客户")
    @PutMapping
    public Result<Void> update(@RequestBody Customer customer) {
        customerService.update(customer);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除客户")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        customerService.delete(id);
        return Result.success("删除成功");
    }
}

